{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pandas import read_csv, set_option\n",
    "from lifelines import KaplanMeierFitter\n",
    "from lifelines.utils import median_survival_times\n",
    "from lifelines import CoxPHFitter\n",
    "from matplotlib import pyplot as plt\n",
    "from toad.detector import detect\n",
    "\n",
    "set_option(\"display.width\", 10000)\n",
    "set_option(\"display.max_rows\", None)\n",
    "set_option(\"display.max_columns\", None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "   customerID  SeniorCitizen  tenure  MonthlyCharges  TotalCharges  gender_Male  Partner_Yes  Dependents_Yes  PhoneService_Yes  MultipleLines_No phone service  MultipleLines_Yes  InternetService_DSL  InternetService_Fiber optic  OnlineSecurity_No internet service  OnlineSecurity_Yes  OnlineBackup_No internet service  OnlineBackup_Yes  DeviceProtection_No internet service  DeviceProtection_Yes  TechSupport_No internet service  TechSupport_Yes  StreamingTV_No internet service  StreamingTV_Yes  StreamingMovies_No internet service  StreamingMovies_Yes  Contract_One year  Contract_Two year  PaperlessBilling_Yes  PaymentMethod_Credit card (automatic)  PaymentMethod_Electronic check  PaymentMethod_Mailed check  Churn_Yes\n0  7590-VHVEG              0       1           29.85         29.85          0.0          1.0             0.0               0.0                             1.0                0.0                  1.0                          0.0                                 0.0                 0.0                               0.0               1.0                                   0.0                   0.0                              0.0              0.0                              0.0              0.0                                  0.0                  0.0                0.0                0.0                   1.0                                    0.0                             1.0                         0.0        0.0\n1  5575-GNVDE              0      34           56.95       1889.50          1.0          0.0             0.0               1.0                             0.0                0.0                  1.0                          0.0                                 0.0                 1.0                               0.0               0.0                                   0.0                   1.0                              0.0              0.0                              0.0              0.0                                  0.0                  0.0                1.0                0.0                   0.0                                    0.0                             0.0                         1.0        0.0\n2  3668-QPYBK              0       2           53.85        108.15          1.0          0.0             0.0               1.0                             0.0                0.0                  1.0                          0.0                                 0.0                 1.0                               0.0               1.0                                   0.0                   0.0                              0.0              0.0                              0.0              0.0                                  0.0                  0.0                0.0                0.0                   1.0                                    0.0                             0.0                         1.0        1.0\n3  7795-CFOCW              0      45           42.30       1840.75          1.0          0.0             0.0               0.0                             1.0                0.0                  1.0                          0.0                                 0.0                 1.0                               0.0               0.0                                   0.0                   1.0                              0.0              1.0                              0.0              0.0                                  0.0                  0.0                1.0                0.0                   0.0                                    0.0                             0.0                         0.0        0.0\n4  9237-HQITU              0       2           70.70        151.65          0.0          0.0             0.0               1.0                             0.0                0.0                  0.0                          1.0                                 0.0                 0.0                               0.0               0.0                                   0.0                   0.0                              0.0              0.0                              0.0              0.0                                  0.0                  0.0                0.0                0.0                   1.0                                    0.0                             1.0                         0.0        1.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>customerID</th>\n      <th>SeniorCitizen</th>\n      <th>tenure</th>\n      <th>MonthlyCharges</th>\n      <th>TotalCharges</th>\n      <th>gender_Male</th>\n      <th>Partner_Yes</th>\n      <th>Dependents_Yes</th>\n      <th>PhoneService_Yes</th>\n      <th>MultipleLines_No phone service</th>\n      <th>MultipleLines_Yes</th>\n      <th>InternetService_DSL</th>\n      <th>InternetService_Fiber optic</th>\n      <th>OnlineSecurity_No internet service</th>\n      <th>OnlineSecurity_Yes</th>\n      <th>OnlineBackup_No internet service</th>\n      <th>OnlineBackup_Yes</th>\n      <th>DeviceProtection_No internet service</th>\n      <th>DeviceProtection_Yes</th>\n      <th>TechSupport_No internet service</th>\n      <th>TechSupport_Yes</th>\n      <th>StreamingTV_No internet service</th>\n      <th>StreamingTV_Yes</th>\n      <th>StreamingMovies_No internet service</th>\n      <th>StreamingMovies_Yes</th>\n      <th>Contract_One year</th>\n      <th>Contract_Two year</th>\n      <th>PaperlessBilling_Yes</th>\n      <th>PaymentMethod_Credit card (automatic)</th>\n      <th>PaymentMethod_Electronic check</th>\n      <th>PaymentMethod_Mailed check</th>\n      <th>Churn_Yes</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>7590-VHVEG</td>\n      <td>0</td>\n      <td>1</td>\n      <td>29.85</td>\n      <td>29.85</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>5575-GNVDE</td>\n      <td>0</td>\n      <td>34</td>\n      <td>56.95</td>\n      <td>1889.50</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3668-QPYBK</td>\n      <td>0</td>\n      <td>2</td>\n      <td>53.85</td>\n      <td>108.15</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>1.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>7795-CFOCW</td>\n      <td>0</td>\n      <td>45</td>\n      <td>42.30</td>\n      <td>1840.75</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>9237-HQITU</td>\n      <td>0</td>\n      <td>2</td>\n      <td>70.70</td>\n      <td>151.65</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n      <td>0.0</td>\n      <td>1.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = read_csv(\"../data/precessed_feature.csv\")\n",
    "data.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "<lifelines.CoxPHFitter: fitted with 7043 total observations, 5174 right-censored observations>\n             duration col = 'tenure'\n                event col = 'Churn_Yes'\n      baseline estimation = breslow\n   number of observations = 7043\nnumber of events observed = 1869\n   partial log-likelihood = -13338.70\n         time fit was run = 2022-03-09 13:33:43 UTC\n\n---\n                 coef  exp(coef)   se(coef)   coef lower 95%   coef upper 95%  exp(coef) lower 95%  exp(coef) upper 95%\ncovariate                                                                                                              \nSeniorCitizen    0.31       1.36       0.05             0.20             0.41                 1.22                 1.51\nMonthlyCharges   0.07       1.07       0.00             0.06             0.07                 1.06                 1.07\nTotalCharges    -0.00       1.00       0.00            -0.00            -0.00                 1.00                 1.00\n\n                    z      p   -log2(p)\ncovariate                              \nSeniorCitizen    5.64 <0.005      25.80\nMonthlyCharges  45.52 <0.005        inf\nTotalCharges   -47.57 <0.005        inf\n---\nConcordance = 0.90\nPartial AIC = 26683.39\nlog-likelihood ratio test = 4628.68 on 3 df\n-log2(p) of ll-ratio test = inf",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <tbody>\n    <tr>\n      <th>model</th>\n      <td>lifelines.CoxPHFitter</td>\n    </tr>\n    <tr>\n      <th>duration col</th>\n      <td>'tenure'</td>\n    </tr>\n    <tr>\n      <th>event col</th>\n      <td>'Churn_Yes'</td>\n    </tr>\n    <tr>\n      <th>baseline estimation</th>\n      <td>breslow</td>\n    </tr>\n    <tr>\n      <th>number of observations</th>\n      <td>7043</td>\n    </tr>\n    <tr>\n      <th>number of events observed</th>\n      <td>1869</td>\n    </tr>\n    <tr>\n      <th>partial log-likelihood</th>\n      <td>-13338.70</td>\n    </tr>\n    <tr>\n      <th>time fit was run</th>\n      <td>2022-03-09 13:33:43 UTC</td>\n    </tr>\n  </tbody>\n</table>\n</div><table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th style=\"min-width: 12px;\"></th>\n      <th style=\"min-width: 12px;\">coef</th>\n      <th style=\"min-width: 12px;\">exp(coef)</th>\n      <th style=\"min-width: 12px;\">se(coef)</th>\n      <th style=\"min-width: 12px;\">coef lower 95%</th>\n      <th style=\"min-width: 12px;\">coef upper 95%</th>\n      <th style=\"min-width: 12px;\">exp(coef) lower 95%</th>\n      <th style=\"min-width: 12px;\">exp(coef) upper 95%</th>\n      <th style=\"min-width: 12px;\">z</th>\n      <th style=\"min-width: 12px;\">p</th>\n      <th style=\"min-width: 12px;\">-log2(p)</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>SeniorCitizen</th>\n      <td>0.31</td>\n      <td>1.36</td>\n      <td>0.05</td>\n      <td>0.20</td>\n      <td>0.41</td>\n      <td>1.22</td>\n      <td>1.51</td>\n      <td>5.64</td>\n      <td>&lt;0.005</td>\n      <td>25.80</td>\n    </tr>\n    <tr>\n      <th>MonthlyCharges</th>\n      <td>0.07</td>\n      <td>1.07</td>\n      <td>0.00</td>\n      <td>0.06</td>\n      <td>0.07</td>\n      <td>1.06</td>\n      <td>1.07</td>\n      <td>45.52</td>\n      <td>&lt;0.005</td>\n      <td>inf</td>\n    </tr>\n    <tr>\n      <th>TotalCharges</th>\n      <td>-0.00</td>\n      <td>1.00</td>\n      <td>0.00</td>\n      <td>-0.00</td>\n      <td>-0.00</td>\n      <td>1.00</td>\n      <td>1.00</td>\n      <td>-47.57</td>\n      <td>&lt;0.005</td>\n      <td>inf</td>\n    </tr>\n  </tbody>\n</table><br><div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <tbody>\n    <tr>\n      <th>Concordance</th>\n      <td>0.90</td>\n    </tr>\n    <tr>\n      <th>Partial AIC</th>\n      <td>26683.39</td>\n    </tr>\n    <tr>\n      <th>log-likelihood ratio test</th>\n      <td>4628.68 on 3 df</td>\n    </tr>\n    <tr>\n      <th>-log2(p) of ll-ratio test</th>\n      <td>inf</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/latex": "\\begin{tabular}{lrrrrrrrrrr}\n\\toprule\n{} &  coef &  exp(coef) &  se(coef) &  coef lower 95\\% &  coef upper 95\\% &  exp(coef) lower 95\\% &  exp(coef) upper 95\\% &      z &    p &  -log2(p) \\\\\ncovariate      &       &            &           &                 &                 &                      &                      &        &      &           \\\\\n\\midrule\nSeniorCitizen  &  0.31 &       1.36 &      0.05 &            0.20 &            0.41 &                 1.22 &                 1.51 &   5.64 & 0.00 &     25.80 \\\\\nMonthlyCharges &  0.07 &       1.07 &      0.00 &            0.06 &            0.07 &                 1.06 &                 1.07 &  45.52 & 0.00 &       inf \\\\\nTotalCharges   & -0.00 &       1.00 &      0.00 &           -0.00 &           -0.00 &                 1.00 &                 1.00 & -47.57 & 0.00 &       inf \\\\\n\\bottomrule\n\\end{tabular}\n"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "cph = CoxPHFitter()\n",
    "dt = data[[\"tenure\", \"Churn_Yes\", \"SeniorCitizen\", \"MonthlyCharges\", \"TotalCharges\"]]\n",
    "cph.fit(dt, 'tenure', event_col='Churn_Yes')\n",
    "print(cph.print_summary())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}